﻿using System;
using System.IO;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using FluxJpeg.Core;
using FluxJpeg.Core.Encoder;

namespace Lib
{
    /// <summary>
    /// by 菩提树下的杨过(yjmyzz@126.com) 允许用于商业用途，但请保留作者信息
    /// </summary>
    public static class Tool
    {
        public static Color GetColorFromString(string colorString)
        {
            Color result = Colors.Black;

            try
            {
                colorString = colorString.ToUpper().Trim('#');
                string a = "0x" + colorString.Substring(0, 2);
                string r = "0x" + colorString.Substring(2, 2);
                string g = "0x" + colorString.Substring(4, 2);
                string b = "0x" + colorString.Substring(6, 2);


                byte A = Convert.ToByte(a, 16);
                byte R = Convert.ToByte(r, 16);
                byte G = Convert.ToByte(g, 16);
                byte B = Convert.ToByte(b, 16);

                result = Color.FromArgb(A, R, G, B);

            }
            catch { }

            return result;
        } 

        

        public static string GetBase64Image(WriteableBitmap bitmap)
        {
            return GetBase64Image(bitmap,75);

        }


        public static string GetBase64Image(WriteableBitmap bitmap,int quality)
        {
            if (quality >= 100) { quality = 100; }
            if (quality <= 0) { quality = 1; }
            int width = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

            for (int i = 0; i < bands; i++)
            {
                raster[i] = new byte[width, height];
            }

            for (int row = 0; row < height; row++)
            {
                for (int column = 0; column < width; column++)
                {
                    int pixel = bitmap.Pixels[width * row + column];
                    raster[0][column, row] = (byte)(pixel >> 16);
                    raster[1][column, row] = (byte)(pixel >> 8);
                    raster[2][column, row] = (byte)pixel;
                }
            }

            ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
            FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
            MemoryStream stream = new MemoryStream();
            JpegEncoder encoder = new JpegEncoder(img, quality, stream);
            encoder.Encode();

            stream.Seek(0, SeekOrigin.Begin);
            byte[] binaryData = new Byte[stream.Length];
            long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);

            string base64String =
                    System.Convert.ToBase64String(binaryData,
                                                  0,
                                                  binaryData.Length);

            return base64String;

        }


        public static string ByteArrayToString(byte[] arrB)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            foreach (byte b in arrB)
            {
                sb.Append(b.ToString() + "，");
            }
            return sb.ToString().Trim(',') + "]";
        }

        public static byte[] JsonStringToByteArray(string jsonByteString)
        {
            jsonByteString = jsonByteString.Substring(1, jsonByteString.Length - 2);
            string[] arr = jsonByteString.Split(',');
            byte[] bResult = new byte[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                bResult[i] = byte.Parse(arr[i]);
            }
            return bResult;
        }

        
    }
}
